package com.donger.business.upms.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.donger.business.upms.entity.SysPost;
import com.donger.business.upms.query.PostQuery;
import com.donger.business.upms.service.SysPostService;
import com.donger.common.core.utils.Res;
import com.donger.common.core.utils.Result;
import com.donger.common.data.entity.DropDown;
import com.donger.common.query.utils.QueryGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;


/**
 * 岗位表(SysPost)表控制层
 *
 * @author 7326
 * @since 2020-05-03 10:28:16
 */
@RestController
@RequestMapping("/sys/post")
@AllArgsConstructor
@Slf4j
@Api(tags = "岗位表开放API")
public class SysPostController {
    /**
     * 服务对象
     */
    private final SysPostService sysPostService;

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param postQuery 查询实体
     * @return 所有数据
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    public Result selectAll(Page<SysPost> page, PostQuery postQuery) {
        QueryWrapper<SysPost> sysPostQueryWrapper = QueryGenerator.<SysPost>initQueryWrapper(postQuery);
        sysPostQueryWrapper.orderByAsc("order_num");
        return Res.ok(sysPostService.page(page, sysPostQueryWrapper));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/info/{id}")
    public Result selectOne(@PathVariable("id") Long id) {
        return Res.ok(sysPostService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param sysPost 实体对象
     * @return 新增结果
     */
    @PostMapping("save")
    public Result insert(@RequestBody SysPost sysPost) {
        return Res.ok(sysPostService.save(sysPost));
    }

    /**
     * 修改数据
     *
     * @param sysPost 实体对象
     * @return 修改结果
     */
    @PostMapping("update")
    public Result update(@RequestBody SysPost sysPost) {
        return Res.ok(sysPostService.updateById(sysPost));
    }

    /**
     * 删除数据
     *
     * @param id 主键结合
     * @return 删除结果
     */
    @DeleteMapping("/del/{id}")
    public Result delete(@PathVariable("id") Long id) {
        return Res.ok(sysPostService.removeById(id));
    }


    /**
     * 下拉
     *
     * @return
     */
    @GetMapping("/dropdown")
    public Result dropdown() {
        List<DropDown> collect = sysPostService.list().stream().map(item -> new DropDown().setValue(item.getId()).setLabel(item.getName()))
                .collect(Collectors.toList());
        return Res.ok(collect);
    }
}